import numpy as np
from pptx import Presentation
from pptx.util import Inches
from pptx.chart.data import BubbleChartData
from pptx.enum.chart import XL_CHART_TYPE

# 创建幻灯片-------------------------------------------------
prs = Presentation()  # 初始化 ppt 文档
title_only_slide_layout = prs.slide_layouts[5]  # 选择空白幻灯片版式
slide = prs.slides.add_slide(title_only_slide_layout)  # 添加一页空白幻灯片
shapes = slide.shapes

shapes.title.text = '气泡图'

# 定义图表数据-------------------------------------------------
np.random.seed(42)
x = 10 * np.random.rand(10)
np.random.seed(2)
y = 20 * np.random.rand(10)
z = 0.5 * x + 0.3 * y

chart_data = BubbleChartData()

series = chart_data.add_series('Model1')
for e in zip(x, y, z):
    print(e)
    series.add_data_point(*e)


np.random.seed(42)
x = 5 * np.random.rand(10)
np.random.seed(2)
y = 10 * np.random.rand(10)
z = 01.* x/x
series = chart_data.add_series('Model2')
for e in zip(x, y, z):
    print(e)
    series.add_data_point(*e)


# 添加图表-------------------------------------------------
left, top, width, height = Inches(0.5), Inches(1.5), Inches(9), Inches(6)
graphic_frame = slide.shapes.add_chart(chart_type=XL_CHART_TYPE.BUBBLE,  # 图表类型
                                       x=left, y=top,  # 图表区的位置
                                       cx=width, cy=height,  # 图表的宽和高
                                       chart_data=chart_data)

# 保存 ppt 文档
prs.save('test.pptx')